package com.jsgt.greekfire.ui.mainassets.coindetail

import android.os.Bundle
import android.text.TextUtils
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseFragment
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.CurrencyDescribe
import com.jsgt.greekfire.factory.KLineViewModelFactory
import com.jsgt.greekfire.viewmodel.KLineViewModel
import com.jsgt.greekfire.viewmodel.TradingViewModel
import kotlinx.android.synthetic.main.fragment_introduce.*

private const val ARG_PARAM_ASSETID = "assetId"
private const val ARG_PARAM_SYMBOL = "symbol"

class CoinIntroductionFragment : BaseFragment() {
    private var symbol: String? = null
    private var assetId: Int? = 0


    private val tradingViewModel: TradingViewModel by lazy {
        ViewModelProvider(this).get(TradingViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            assetId = it.getInt(ARG_PARAM_ASSETID)
            symbol = it.getString(ARG_PARAM_SYMBOL)
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(assetId: Int, param1: String) =
            CoinIntroductionFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_PARAM_ASSETID, assetId)
                    putString(ARG_PARAM_SYMBOL, param1)
                }
            }
    }


    override fun getLayoutId(): Int = R.layout.fragment_coin_introduction


    override fun main() {
        tradingViewModel.coinIntroduceLiveData.observe(this, Observer {
            if (it != null) {
                txvName.text = it.coinName
                txvCreateTime.text = it.createTime.substring(0, 10)
                txvTotal.text = it.issueAmount
                txvCirculation.text = it.circulationAmount
                txvPrice.text = it.crowdFunding
                txvWhitePaper.text = it.whitePaper
                txvOfficialWebsite.text = it.officialWebsite
                txvBlockQuery.text = it.blockQuery
                txvCoinIntroduce.text = it.coinIntroduce
            }
        })
        if (!TextUtils.isEmpty(symbol)) {
            tradingViewModel.getCoinIntroduce(symbol!!)
        }
    }
}